package com.dream.testtubebaby.fragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dream.testtubebaby.R;
import com.dream.testtubebaby.activity.MyOrderActivity;
import com.dream.testtubebaby.activity.MyShoppingCartActivity;
import com.dream.testtubebaby.activity.ProductCarRentalDetailActivity;
import com.dream.testtubebaby.activity.ProductCarRentalListActivity;
import com.dream.testtubebaby.activity.ProductConfmtCenterDetailActivity;
import com.dream.testtubebaby.activity.ProductConfmtCenterListActivity;
import com.dream.testtubebaby.adapter.ProductClassifyAdapter;
import com.dream.testtubebaby.adapter.ProductViewCarRentalAdapter;
import com.dream.testtubebaby.adapter.ProductViewConfmtCenterAdapter;
import com.dream.testtubebaby.data.ADInfo;
import com.dream.testtubebaby.data.product.CarRentalListItemData;
import com.dream.testtubebaby.data.product.ConfmtCenterListItemData;
import com.dream.testtubebaby.data.product.ProductClassifyData;
import com.dream.testtubebaby.fragment.CycleViewPager.ImageCycleViewListener;
import com.dream.testtubebaby.net.ServerStub;
import com.dream.testtubebaby.okhttp.OkHttpClientManager;
import com.dream.testtubebaby.okhttp.OkHttpClientManager.Param;
import com.dream.testtubebaby.utils.ToastUtil;
import com.dream.testtubebaby.utils.ViewFactory;
import com.squareup.okhttp.Request;

public class ProductFragment extends Fragment implements OnClickListener {
	private View rootView;

	private CycleViewPager cycleViewPager;
	private int advertImageViewArray[] = { R.drawable.show_m1,
			R.drawable.menu_viewpager_1, R.drawable.menu_viewpager_2,
			R.drawable.menu_viewpager_3, R.drawable.menu_viewpager_4,
			R.drawable.menu_viewpager_5 };
	private List<ImageView> views = new ArrayList<ImageView>();
	private List<ADInfo> infos = new ArrayList<ADInfo>();

	private GridView product_gridview;
	private ProductClassifyAdapter mProductClassifyAdapter;
	private List<ProductClassifyData> mProductClassityDatas;
	private int productClassityResId[] = { R.drawable.product_classity_1,
			R.drawable.product_classity_1, R.drawable.product_classity_1,
			R.drawable.product_classity_1, R.drawable.product_classity_1,
			R.drawable.product_classity_1, R.drawable.product_classity_1,
			R.drawable.product_classity_1 };

	private ArrayList<ConfmtCenterListItemData> mConfmtCenterListDatas = new ArrayList<ConfmtCenterListItemData>();
	private ArrayList<CarRentalListItemData> mCarRentalListDatas = new ArrayList<CarRentalListItemData>();
	private ListView confmtcenter_view_list;
	private ListView carrental_view_list;
	private ProductViewConfmtCenterAdapter mProductViewConfmtCenterAdapter;
	private ProductViewCarRentalAdapter mProductViewCarRentalAdapter;
	private TextView confmtcenter_more_tv;
	private TextView carrental_more_tv;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (rootView == null) {
			rootView = inflater.inflate(R.layout.product_fragment_view, null);
			initCycleView();
			initGridview();
			initListView();
		}
		ViewGroup parent = (ViewGroup) rootView.getParent();
		if (parent != null) {
			parent.removeView(rootView);
		}

		return rootView;
	}

	private void initGridview() {
		product_gridview = (GridView) rootView
				.findViewById(R.id.product_gridview);
		product_gridview.setSelector(new ColorDrawable(Color.TRANSPARENT));
		mProductClassityDatas = new ArrayList<ProductClassifyData>();
		String product_classity_strings[] = getResources().getStringArray(
				R.array.product_classity_arr);
		for (int i = 0; i < product_classity_strings.length; i++) {
			ProductClassifyData productClassifyData = new ProductClassifyData();
			productClassifyData.setTitle(product_classity_strings[i]);
			productClassifyData.setRes_id(productClassityResId[i]);
			mProductClassityDatas.add(productClassifyData);
		}
		mProductClassifyAdapter = new ProductClassifyAdapter(getActivity(),
				mProductClassityDatas, R.layout.product_classity_grid_item);
		product_gridview.setAdapter(mProductClassifyAdapter);
		product_gridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (position == 0) {
					Intent intent = new Intent(getActivity(),
							ProductCarRentalListActivity.class);
					startActivity(intent);
				} else if (position == 1) {
					Intent intent = new Intent(getActivity(),
							ProductConfmtCenterListActivity.class);
					startActivity(intent);
				} else if (position == 6) {
					Intent intent = new Intent(getActivity(),
							MyShoppingCartActivity.class);
					startActivity(intent);
				} else if (position == 7) {
					Intent intent = new Intent(getActivity(),
							MyOrderActivity.class);
					startActivity(intent);
				}
			}
		});
	}

	private void initCycleView() {
		cycleViewPager = (CycleViewPager) getActivity().getFragmentManager()
				.findFragmentById(R.id.fragment_product_viewpager_content);

		for (int i = 0; i < advertImageViewArray.length; i++) {
			ADInfo info = new ADInfo();
			// info.setUrl(imageUrls[i]);
			info.setId(advertImageViewArray[i]);
			info.setContent("ͼƬ-->" + i);
			infos.add(info);
		}
		views.add(ViewFactory.getImageView(getActivity(),
				infos.get(infos.size() - 1).getId()));
		for (int i = 0; i < infos.size(); i++) {
			views.add(ViewFactory.getImageView(getActivity(), infos.get(i)
					.getId()));
		}
		views.add(ViewFactory.getImageView(getActivity(), infos.get(0).getId()));
		cycleViewPager.setCycle(true);
		cycleViewPager.setData(views, infos, mAdCycleViewListener);
		cycleViewPager.setWheel(true);
	}

	private ImageCycleViewListener mAdCycleViewListener = new ImageCycleViewListener() {

		@Override
		public void onImageClick(ADInfo info, int position, View imageView) {
			if (cycleViewPager.isCycle()) {
				position = position - 1;
			}
		}
	};

	private void initListView() {
		confmtcenter_view_list = (ListView) rootView
				.findViewById(R.id.confmtcenter_view_list);
		carrental_view_list = (ListView) rootView
				.findViewById(R.id.carrental_view_list);
		mProductViewConfmtCenterAdapter = new ProductViewConfmtCenterAdapter(
				getActivity(), mConfmtCenterListDatas,
				R.layout.product_view_confmtcenter_view);
		mProductViewCarRentalAdapter = new ProductViewCarRentalAdapter(
				getActivity(), mCarRentalListDatas,
				R.layout.product_view_carrental_view);
		confmtcenter_view_list.setAdapter(mProductViewConfmtCenterAdapter);
		carrental_view_list.setAdapter(mProductViewCarRentalAdapter);
		confmtcenter_view_list
				.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						Intent intent = new Intent(getActivity(),
								ProductConfmtCenterDetailActivity.class);
						long product_id = mConfmtCenterListDatas.get(position)
								.getId();
						intent.putExtra(
								ProductConfmtCenterDetailActivity.PRODUCT_DETAIL_ID,
								product_id);
						startActivity(intent);
					}
				});
		carrental_view_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(getActivity(),
						ProductCarRentalDetailActivity.class);
				intent.putExtra(
						ProductCarRentalDetailActivity.PRODUCT_DETAIL_ID,
						mCarRentalListDatas.get(position).getId());
				startActivity(intent);
			}
		});
		downLoadConfmtCenterListItem();
		downLoadCarRentalListItem();

		confmtcenter_more_tv = (TextView) rootView
				.findViewById(R.id.confmtcenter_more_tv);
		carrental_more_tv = (TextView) rootView
				.findViewById(R.id.carrental_more_tv);
		confmtcenter_more_tv.setOnClickListener(this);
		carrental_more_tv.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.confmtcenter_more_tv: {
			Intent intent = new Intent(getActivity(),
					ProductConfmtCenterListActivity.class);
			startActivity(intent);

			break;
		}
		case R.id.carrental_more_tv: {
			Intent intent = new Intent(getActivity(),
					ProductCarRentalListActivity.class);
			startActivity(intent);
		}
			break;
		default:
			break;
		}
	}

	private void downLoadConfmtCenterListItem() {
		Param[] params = new Param[2];
		params[0] = new Param("step", String.valueOf(3));
		params[1] = new Param("fromid", String.valueOf(0));
		OkHttpClientManager.postAsyn(ServerStub.BASEURL
				+ "m=Home&c=App&a=GetPNCareCenterList",
				new OkHttpClientManager.ResultCallback<String>() {

					@Override
					public void onError(Request request, Exception e) {
						ToastUtil.toast(getActivity(), R.string.server_error);
					}

					@Override
					public void onResponse(String response) {
						try {
							JSONObject obj = new JSONObject(response);
							if (obj != null) {
								int result = obj.getInt("result");
								if (result == 0) {
									JSONArray jsonProduct = new JSONArray(obj
											.getString("CareCenterList"));
									for (int i = 0; i < jsonProduct.length(); i++) {
										JSONObject jsonObject = (JSONObject) jsonProduct
												.get(i);
										ConfmtCenterListItemData itemData = ConfmtCenterListItemData
												.getProductListItemDataNode(jsonObject);
										mConfmtCenterListDatas.add(itemData);
									}
									mProductViewConfmtCenterAdapter
											.notifyDataSetChanged();
								}
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				}, params);
	}

	private void downLoadCarRentalListItem() {
		Param[] params = new Param[2];
		params[0] = new Param("step", String.valueOf(3));
		params[1] = new Param("fromid", String.valueOf(0));
		OkHttpClientManager.postAsyn(ServerStub.BASEURL
				+ "m=Home&c=App&a=GetCarRentalList",
				new OkHttpClientManager.ResultCallback<String>() {

					@Override
					public void onError(Request request, Exception e) {
						ToastUtil.toast(getActivity(), R.string.server_error);
					}

					@Override
					public void onResponse(String response) {
						try {
							JSONObject obj = new JSONObject(response);
							if (obj != null) {
								int result = obj.getInt("result");
								if (result == 0) {
									JSONArray jsonProduct = new JSONArray(obj
											.getString("CarRentalList"));
									for (int i = 0; i < jsonProduct.length(); i++) {
										JSONObject jsonObject = (JSONObject) jsonProduct
												.get(i);
										CarRentalListItemData itemData = CarRentalListItemData
												.getProductListItemDataNode(jsonObject);
										mCarRentalListDatas.add(itemData);
									}
									mProductViewCarRentalAdapter
											.notifyDataSetChanged();
								}
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				}, params);
	}
}
